package com.threadExample;

/**
 * Created by yijy on 2017/7/26.
 */
public class Stack {
    private String name;
    private String[] buffer = new String[100];
    int point=-1;

    public Stack(String name){
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public synchronized int getPoint() {
        return point;
    }

    public synchronized String pull(){
        this.notifyAll();
        while (point==-1){
            System.out.println(getName() +": goods don't have,please wait!");
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw  new RuntimeException();
            }
        }
        String goods = buffer[point];
        buffer[point]=null;
        Thread.yield();
        point--;
        return goods;
    }

    public synchronized void push(String goods){
        this.notifyAll();
        while (point==buffer.length-1){
            System.out.println(getName()+": goods is full,please wait! ");
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw  new RuntimeException();
            }
        }
        point++;
        Thread.yield();
        buffer[point]=goods;
    }
}
